﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlgoTest
{
    class TreeNode<T>
    {
        public T Value;
        public TreeNode<T> Left;
        public TreeNode<T> Right;

        /// <summary>
        /// Traverses the node and all it's children using a breadth-first algorithm
        /// and returns their values as IEnumerable
        /// </summary>
        /// <returns></returns>
        public IEnumerable<T> AsEnumerable()
        {
            var queue = new Queue<TreeNode<T>>();
            queue.Enqueue(this);

            while (queue.Count > 0)
            {
                var curNode = queue.Dequeue();

                yield return curNode.Value;

                if (curNode.Left != null)
                    queue.Enqueue(curNode.Left);
                if (curNode.Right != null)
                    queue.Enqueue(curNode.Right);
            }
        }
    }
}
